<script setup>
import {
  getBizFriendsLinks,
  addBizFriendsLinks,
  updateBizFriendsLinks,
  findMaxSort
} from "@/api/biz/bizFriendsLinks";
import storage from "@/utils/storage";
import { TOKEN } from "@/store/mutation-types";
const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {}
});
const fileHeahers = ref({
  url: import.meta.env.VITE_APP_API_BASEURL + "/common/upload",
  headers: {
    Authorization: "Bearer " + storage.local.get(TOKEN)
  }
});
onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort().then((res) => {
      data.value.form.sort = res.data;
    });
  }
});

function getInfo() {
  data.value.loading = true;
  getBizFriendsLinks(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}
function handleAvatarSuccess(res, file) {
  console.log(file.response.data.url);
  data.value.form.icon = file.response.data.url;
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          addBizFriendsLinks(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizFriendsLinks(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="图标" prop="icon">
            <el-upload
              class="avatar-uploader"
              :action="fileHeahers.url"
              :headers="fileHeahers.headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="data.form.icon" :src="data.form.icon" class="avatar" />

              <svg-icon
                v-else
                class="el-icon-plus avatar-uploader-icon"
                name="ep:picture-rounded"
              />
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="小程序名称" prop="name">
            <el-input placeholder="请输入小程序名称" v-model="data.form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="APPID" prop="appid">
            <el-input
              placeholder="请输入小程序APPID"
              v-model="data.form.appid"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="链接地址" prop="icon">
            <el-input placeholder="请输入链接地址" v-model="data.form.links" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="点击次数" prop="clicks">
            <el-input-number
              placeholder="点击次数"
              v-model="data.form.clicks"
              min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="status">
            <el-input-number placeholder="排序" v-model="data.form.sort" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否显示" prop="status">
            <el-switch
              v-model="data.form.status"
              active-text="否"
              inactive-text="是"
              active-value="1"
              inactive-value="0"
              inline-prompt
              style="
                --el-switch-off-color: #409eff;
                --el-switch-on-color: #ff4949;
              "
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              type="text-area"
              placeholder="请输入备注信息"
              v-model="data.form.remark"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<style scoped>
.avatar-uploader .el-upload {
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 78px;
  height: 78px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 38px;
  color: #409eff;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 58px;
  height: 58px;
  display: block;
}
</style>
